package com.happycolor.littlelove;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.happycolor.littlelove.base.BasicActivity;
import com.happycolor.littlelove.base.Constant;
import com.happycolor.littlelove.base.HCLog;
import com.happycolor.littlelove.base.LBSLocation;
import com.happycolor.littlelove.base.LittleLoveApplication;

public class MainActivity extends BasicActivity {
	
	private static final HCLog log = HCLog.getInstance("Main");
	private Dialog loadingDialog;
	private boolean showLoading = true;
	private int mScreenHeight;
	private TextView userInfoText;
	private String location=new String();
	private DateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	@SuppressLint("HandlerLeak")
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constant.MSG_LOADING_FINISHED:
				loadingDialog.dismiss();
				break;
			case Constant.MSG_REFRESH:
				MainActivity.this.refreshLocationInfo();
			default:
				break;
			}
		}
	};
	private void refreshLocationInfo() {
		 LittleLoveApplication app = (LittleLoveApplication) this
		 .getApplication();
		 LBSLocation.getInstance(app).startLocation();
		if(app.lbsInfo!=""&&!location.equals(app.lbsInfo)) {
			location=app.lbsInfo;
			Date date=new Date();
			userInfoText.setText(userInfoText.getText()+"\n"+format.format(date)+","+location+"@poi "+app.poiInfo);
		}
		mHandler.sendEmptyMessageDelayed(Constant.MSG_REFRESH, 5000);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		if (showLoading) {
			showLoading = false;
			showLoadingView();
		}
		init();
	}

	private void init() {
		mHandler.sendEmptyMessage(Constant.MSG_REFRESH);
		initScreenDimention();
		userInfoText=(TextView)this.findViewById(R.id.userInfoText);
		LBSLocation.getInstance((LittleLoveApplication) this
				 .getApplication()).startLocation();
	}

	private void initScreenDimention() {
		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		this.mScreenHeight = dm.heightPixels;
	}

	private void showLoadingView() {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.loading, null);
		loadingDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		loadingDialog.setContentView(layout);
		loadingDialog.show();
		ImageView image = (ImageView) layout.findViewById(R.id.image1);
		// ImageView imageBG = (ImageView) layout.findViewById(R.id.image2);
		layout.setBackgroundResource(R.drawable.index);
		image.setBackgroundResource(R.drawable.loading_bird);
		TranslateAnimation animation = new TranslateAnimation(-200, 200, 0, 0);
		animation.setDuration(5000);
		animation.setFillAfter(true);
		image.startAnimation(animation);
		mHandler.sendEmptyMessageDelayed(Constant.MSG_LOADING_FINISHED, 5000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
